package com.broada.apm.ajax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.broada.apm.entity.SSLCertificate;
import com.broada.apm.entity.Service;
import com.broada.apm.entity.ServiceLua;
import com.broada.apm.util.Constants;

public class ServerResult extends Result {

	/**
	 * @Fields serialVersionUID :
	 */

	private static final long serialVersionUID = 1L;

	public void setNodesPage(int pageSize, int pageIndex, List<Service> list) {
		if (list == null || list.size() == 0) {
			put(Constants.NODES, new ArrayList<Service>());
			return;
		}
		int totalPage = 0;
		int count = list.size();
		int number = count % pageSize;
		this.setPageSize(pageSize);
		this.setTotal(count);
		this.setPageIndex(pageIndex);
		if (number > 0) {
			totalPage = count / pageSize + 1;
		} else {
			totalPage = count / pageSize;
		}
		this.setTotalPage(totalPage);
		if (pageIndex < totalPage - 1) {
			list = list.subList(pageIndex * pageSize, (pageIndex + 1) * pageSize);
		} else {
			list = list.subList((totalPage - 1) * pageSize, list.size());
		}
		put(Constants.NODES, list);
	}

	public void setNode(Service service){
		this.put(Constants.NODE, service);
	}

	/**
	 * 如果luaScript为空：{"result": {"lua": null}}
	 * 如果luaScript不为空：{"result":{"serviceId":"ifnjotcl-2-e245879c","lua":"ttt"}}
	 * @param luaScript
	 */
	public void setLuaScript(ServiceLua luaScript) {
		if (luaScript == null) {
			Map<String, ServiceLua> luaMap = new HashMap<String, ServiceLua>();
			luaMap.put("lua", luaScript);
			this.put("result", luaMap);
		} else {
			this.put("result", luaScript);
		}
		
	}

	public void setSSLCeritficate(SSLCertificate sslCertificate) {
		this.put("result", sslCertificate);
	}
	
	public void setServiceFocusCount(Map<String, Object> map){
		this.put("serviceFocusCount", map);
	}
	
	public void setServiceState(List<Map<String, Object>> list) {
		this.put("perfState", list);
	}
	
	public void setServiceAlertMap(List<Map<String, Object>> list) {
		this.put("serviceAlertMap", list);
	}
}
